<?php

$event = new StdClass;
$event->type = null;

echo($this->buffer);

if(!($this->state & self::ONLINE)) {
	if($this->state & self::PENDING) {
		list($server,$num) = explode(' ',$this->buffer);
		if($num == '001') {
			$this->state = (self::CONNECTED|self::ONLINE);
			lcars::$w->text1->buffer->append(lcars::date()." Connected to {$this->network}.");
			$this->whoami = $this->nick;
			
			foreach($this->channels as $chan) {
				$chan = strtolower($chan);
				$this->channel[$chan] = new ircChannel($chan,$this);
			}			
		}
		
		if($server == 'PING') {
			$this->writeNetwork("PONG {$num}");
		}
	}
} else {
	//. normal operations.
	if(preg_match('/^([^\s]+) ([^\s]+) :?([^\s]+)(?: :?(.+))?/',trim($this->buffer),$match)) {
	
		if(!preg_match('/:(.+?)!(.+?)@(.+)/',$match[1],$who)) {
			$who = array($match[1],$match[1],$match[1],$match[1]);
		}
	
		switch($match[2]) {
		
			case('353'): {
				//. recieving nicklist.
				list($channel,$users) = explode(' :',trim($match[4]));
				$channel = strtolower(ltrim($channel,' =@%&~+'));
				$list = explode(' ',$users);
				foreach($list as $usr) {
					$mode = substr($usr,0,1);
					if($mode == '+' || $mode == '@' ||$mode == '%' ||$mode == '&' ||$mode == '~') {
						$niceusr = ltrim($usr,' +@%&~');
					} else {
						$mode = '';
						$niceusr = $usr;
					}
					$niceusr = ltrim($usr,' +%@~');
					if(!array_key_exists($niceusr,$this->channel[$channel]->nicklist)) {
						$this->channel[$channel]->nicklist[strtolower($niceusr)] = new ircNick(
							$niceusr,
							ircNick::getModeFromChar($mode),
							lcars::$w->addNickButton($this->channel[$channel],$niceusr)
						);
					}
					
					if(strtolower($niceusr) == strtolower($this->whoami)) {
						lcars::$w->img['btn-chnick']->setColour(ircNick::colour(ircNick::getModeFromChar($mode)),true);
					}
				}
				
				lcars::$w->sortNickList($this->channel[$channel]);
				break;
			}

	
			case('JOIN'): {
				if(strtolower($who[1]) == strtolower($this->whoami)) {
					$match[3] = strtolower($match[3]);
					if(array_key_exists(strtolower($match[3]),$this->channel)) {
						$this->channel[strtolower($match[3])]->create();
					} else {
						$this->writeNetwork('PART {$match[3]}');
					}
				} else {
					$this->channel[strtolower($match[3])]->terminal->append(sprintf(
						'%s * %s has joined this channel.',
						lcars::date(),
						$who[1]				
					));
				}

				$event = new ircEvent(
					ircEvent::JOIN,
					$this,
					array(
						'nick' => $who[1],
						'host' => $who,
						'channel' => $match[3]									
					)								
				);
				break;
			}
			
			case('MODE'): {
				$this->channel[strtolower($match[3])]->terminal->append(sprintf(
					'%s * %s set modes: %s %s',
					lcars::date(),
					$who[1],
					$match[3],
					$match[4]
				));

				$items = explode(' ',$match[4]);
				$modes = $items[0];
				unset($items[0]);
				
				foreach($items as $index => $towho) {
					if(array_key_exists(strtolower($towho),$this->channel[strtolower($match[3])]->nicklist)) {
						$nick = $this->channel[strtolower($match[3])]->nicklist[strtolower($towho)];
						$char = substr($modes,($index),1);
						if(substr($modes,0,1) == '+') {
							if(!($nick->mode & ($mode = ircNick::getModeFromChar($char)))) {
								$nick->mode += $mode;
							}
						} else {
							if($nick->mode & ($mode = ircNick::getModeFromChar($char))) {
								$nick->mode -= $mode;
							}						
						}

						$nick->updateMode();
						if(strtolower($towho) == strtolower($this->whoami)) {
							lcars::$w->img['btn-chnick']->setColour(ircNick::colour($nick->mode),true);
						}	
					}
				} unset($index,$towho,$nick,$modes,$char,$mode);
				
				$event = new ircEvent(
					ircEvent::MODE,
					$this,
					array(
						'nick' => $who[1],
						'host' => $who,
						'channel' => $match[3],
						'string' => $match[4]									
					)								
				);				
				break;
			}

			case('NICK'): {
				if(strtolower($who[1]) == strtolower($this->whoami)) {
					lcars::$w->text1->buffer->append(sprintf(
						'%s %s => You are now known as %s',
						lcars::date(),
						$this->network,
						$match[3]					
					));	
					
					$this->whoami = $match[3];
					lcars::$w->img['btn-chnick']->setText($match[3]);
				} else {
					lcars::$w->text1->buffer->append(sprintf(
						'%s %s => %s is now known as %s',
						lcars::date(),
						$this->network,
						$who[1],
						$match[3]					
					));
				}

				foreach($this->channel as $chan) {
					foreach($chan->nicklist as $id => $nick) {
						if(strtolower($id) == strtolower($who[1])) {
							$nick->nick = $match[3];
							$nick->button->setText($match[3]);
							$chan->nicklist[strtolower($match[3])] = $nick;
							unset($chan->nicklist[$id]);
						}
					}
				} unset($chan,$nick,$id);

				$event = new ircEvent(
					ircEvent::NICK,
					$this,
					array(
						'oldnick' => $who[1],
						'host' => $who,
						'newnick' => $match[3]								
					)								
				);
				break;
			}

			case('PART'): {
				if(strtolower($who[1]) == strtolower($this->whoami)) {
					if(array_key_exists(strtolower($match[3]),$this->channel)) {
						$this->writeNetwork("JOIN {$match[3]}");
//						$match[3] = strtolower($match[3]);
//						$this->channel[strtolower($match[3])]->destroy();
					}
				} else {
					$this->channel[strtolower($match[3])]->terminal->append(sprintf(
						'%s * %s has left this channel.',
						lcars::date(),
						$who[1]				
					));
				}

				$event = new ircEvent(
					ircEvent::PART,
					$this,
					array(
						'nick' => $who[1],
						'host' => $who,
						'channel' => $match[3]									
					)								
				);
				break;
			}
			
			case('PRIVMSG'): {
			
				if(strtolower($match[3]) == strtolower($this->whoami)) {

					if(preg_match("/^\001(.+)\001/",$match[4],$ctcp)) {
						$event = new ircEvent(
							ircEvent::CTCP,
							$this,
							array(
								'nick' => $who[1],
								'host' => $who,
								'ctcp' => $ctcp[1]									
							)								
						);					
					}

				} else {
				
					if(preg_match("/^\001(.+)\001/",$match[4],$ctcp)) {
						if(substr($ctcp[1],0,6) == 'ACTION') {
							$this->channel[strtolower($match[3])]->terminal->append(sprintf(
								'%s * %s %s',
								lcars::date(),
								$who[1],
								preg_replace("/^\001ACTION |\001/",'',$match[4])				
							));
							
							$event = new ircEvent(
								(ircEvent::MESSAGE | ircEvent::ACTION),
								$this,
								array(
									'nick' => $who[1],
									'host' => $who,
									'channel' => $match[3],
									'ctcp' => $ctcp[1]									
								)								
							);

							if(
								strtolower(lcars::$w->currentChannel->channel) != strtolower($match[3]) ||
								$this->channel[strtolower($match[3])]->network->network != lcars::$w->currentChannel->network->network
							) {
								$this->channel[strtolower($match[3])]->button->setAttn(true);
							}							
					
						} else {
							$event = new ircEvent(
								ircEvent::CTCP,
								$this,
								array(
									'nick' => $who[1],
									'host' => $who,
									'ctcp' => $ctcp[1]									
								)								
							);
						}			
					} else {					
						$this->channel[strtolower($match[3])]->terminal->append(sprintf(
							'%s %s: %s',
							lcars::date(),
							$who[1],
							$match[4]					
						));
			
						$event = new ircEvent(
							ircEvent::MESSAGE,
							$this,
							array(
								'nick' => $who[1],
								'host' => $who,
								'channel' => $match[3],
								'message' => $match[4]									
							)								
						);
						
						if(
							strtolower(lcars::$w->currentChannel->channel) != strtolower($match[3]) ||
							$this->channel[strtolower($match[3])]->network->network != lcars::$w->currentChannel->network->network
						) {
							$this->channel[strtolower($match[3])]->button->setAttn(true);
						}
					}			
				}
							
				break;
			}				
		
			default: {
				echo("unknown match: {$match[2]}: {$this->buffer}\n");
			}
		}
	
		$match = null;
	}
	
	else if(preg_match('/^([^\s]+) :?([^\s]+)/',trim($this->buffer),$match)) {
		if($match[1] == 'PING') {
			lcars::$w->text1->buffer->append(lcars::date()." {$this->network} => PING");
			$this->writeNetwork("PONG :{$match[2]}");
		}
	}
	
	else {
		echo("unmatched data: {$this->buffer}\n");
	}
	
	ircEvent::runScripts($event);	
	
}

?>